package com.sw.bbs.common.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * SwaggerConfiguration
 *
 * @author Johnathan
 **/
@EnableSwagger2
@Configuration
public class SwaggerConfiguration {

    @Bean
    public Docket createSwaggerDocket() {
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> parameters = new ArrayList<>();

        parameterBuilder.name("accessToken")
                .description("user's access token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();
        parameters.add(parameterBuilder.build());

        ApiInfo apiInfo = new ApiInfo(
                "Cat-BBS RESTFul API",
                "调试API",
                "1.0",
                "TERMS OF SERVICE URL",
                new Contact("SamWuu","SamWuu@sw.com","iamkeason@gmail.com"),
                "",
                "",
                Collections.emptyList()
        );

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.sw.bbs"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(parameters);
    }
}
